<?php
/**
 * The File Validator
 * 
 * This validator validates uploaded files against a list of mimetypes and a maximum 
 * file size.
 * 
 * @package auxo
 * @subpackage Validator
 * @version $Id$	
 * @copyright Copyright belongs to the respective authors
 * @author andreas.horn@extronaut.de
 * @license http://opensource.org/licenses/gpl-license.php GNU Public License, version 2
 */
class tx_auxo_FileValidator extends tx_auxo_validator {
	
	protected $mimetypes;
	protected $maxSize;
	protected $extension;
	
	
	public function __construct($mimetypes, $maxSize) {
		$this->mimetypes = $mimetypes;
		$this->maxSize = $maxSize;
		$this->extension = tx_auxo_context::getService('controller')->getExtension();
	}
	
	private function hasValidMimeType($filename) {
		// verify mime type of file
		$type = $_FILES[$this->extension]['type'][$filename];
		$type = substr($type, strrpos($type,'/')+1);
		if (!strstr($this->mimetypes, $type)) {
			return false;
		}
		
		return true;
	}
	
	private function hasValidSize($filename) {
		if ($_FILES[$this->extension]['size'][$filename] > $this->maxSize) {
			return false;
		}
		return true;
	}
	
	public function validate($filename) {
		if ($this->hasValidMimeType($filename) && $this->hasValidSize($filename)) {
			return true;
		}
	}
}
?>